/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.activity;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.flan.stock.R;
import com.flan.stock.adapter.NavigatorFragmentAdapter;
import com.flan.stock.fragment.FragmentNavigatorA;
import com.flan.stock.fragment.FragmentNavigatorB;
import com.flan.stock.service.StockService;
import com.flan.stock.view.FooterMenuView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * app首页界面
 * @author flan
 * @date   2015年10月5日
 */
public class HomeActivity extends FragmentActivity {
	
	private boolean isExit = false;
	private ViewPager viewPager;
	private FooterMenuView menu;
	private ImageView[] dots;
	//顶部导航处当前小圆点的坐标
	private int navCurrentIndex = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_home);
		viewPager = (ViewPager) this.findViewById(R.id.vp_fragment);
		
		ArrayList<Fragment> fragmentList = new ArrayList<>();
		FragmentNavigatorA fragmenta = new FragmentNavigatorA(this);
		FragmentNavigatorB fragmentb = new FragmentNavigatorB(this);
		fragmentList.add(fragmenta);
		fragmentList.add(fragmentb);
		
		viewPager.setAdapter(new NavigatorFragmentAdapter(getSupportFragmentManager(), fragmentList));
		viewPager.addOnPageChangeListener(new NavigatorPageChangeListener());
		
		initDot();
		
		//初始化底部菜单控件
		menu = (FooterMenuView) this.findViewById(R.id.footer_menu);
		menu.setCurrentMenu(R.id.menu_home);
		menu.setOnTouchListener(menu.new MenuOnTouchListener());
		
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		menu.setCurrentMenu(R.id.menu_home);
	}
	
	class NavigatorPageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int position) {
			setCurrentDot(position);
		}
		
	}
	
	
	
	//初始化底部小圆点
	private void initDot(){
		
		LinearLayout ll = (LinearLayout) this.findViewById(R.id.lay_dot);
		
		int size = viewPager.getAdapter().getCount();
		dots = new ImageView[size];
		
		for (int i = 0; i < dots.length; i++) {
			
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setTag(i);
			//dots[i].setEnabled(true);
			//给下方小圆点绑定点击事件
			dots[i].setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					int position = (int) v.getTag();	//获得点击的小圆点位置
					
					setCurrentDot(position);
					setCurrentView(position);
				}
			});
		}
		
		dots[navCurrentIndex].setEnabled(false);	//初始化时默认选中第一个小圆点
	}
	
	//设置小圆点被选中状态
	private void setCurrentDot(int position){
		
		if(position < 0 || position >= viewPager.getAdapter().getCount() || position == navCurrentIndex){
			return;
		}
		
		dots[position].setEnabled(false);
		dots[navCurrentIndex].setEnabled(true);
		
		navCurrentIndex = position;
	}
	
	//点击小圆点后切换到响应的页面
	private void setCurrentView(int position){
		
		if(position < 0 || position > viewPager.getAdapter().getCount()){
			return;
		}
		
		viewPager.setCurrentItem(position);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode == KeyEvent.KEYCODE_BACK){
			exitBy2Click();
		}
		return false;
	}
	
	/**
	 * 双击退出程序
	 */
	private void exitBy2Click(){
		
		if(isExit == false){
			isExit = true;
			Toast.makeText(this, "再点一次退出", Toast.LENGTH_SHORT).show();
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					
				}
			}, 2000);// 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
		}else {
			
			//关闭后台服务
			this.stopService(new Intent(this, StockService.class));
			this.finish();
			System.exit(0);
		}
	}
	
}



















